package beans;

import java.net.MalformedURLException;
import java.net.URL;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import model.Host;
import webservice.NodeCommunicationWS;
@Singleton
@Startup
public class MDBListener implements MessageListener {
	
	private NodeCommunicationWS service;
	private TopicConnection connection;
	private TopicSession session;
	private MessageConsumer consumer;
	
	@Resource(mappedName = "java:/ConnectionFactory")
	private ConnectionFactory connectionFactory;
    
    @Resource(mappedName = "java:/topic/mojTopic")
    private Topic topic;
    
    @EJB
    private ChatDataLocal chatData;
    
    public MDBListener() {

    }

	@Override
	public void onMessage(Message arg0) {
		// TODO Auto-generated method stub
		if(arg0 instanceof ObjectMessage) {
			ObjectMessage objMsg = (ObjectMessage) arg0;
			model.Message poruka = null;
			try {
				poruka = (model.Message) objMsg.getObject();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//ukoliko je host posiljaoca isti kao i lokalni host
			if(poruka.getFrom().getHost().equals(chatData.getLocalHost())) {
				String httpHeader = chatData.getHttpHeader();
				String wsdlLocation = chatData.getWsdlLocation();
				QName serviceName = chatData.getServiceName();
				
				//ukoliko je to==null(poruka ide svima osim sebi)
				if(poruka.getTo()==null) {
					for(Host h : chatData.getHosts()) {
						if(!h.equals(chatData.getLocalHost())){
							
							try {
								Service temp = Service
										.create(new URL(httpHeader+h.getAddress()+wsdlLocation), serviceName);
								service = temp.getPort(NodeCommunicationWS.class);
								
								
								service.publish(poruka);

							} catch (MalformedURLException e) {
								e.printStackTrace();
							}
							
						}
					}
				} 
				//ili ako je to.host sa drugog servera, poruka ide samo njemu
				else if(!poruka.getTo().getHost().equals(chatData.getLocalHost())) {
					try {
						Service temp = Service
								.create(new URL(httpHeader+poruka.getTo().getHost().getAddress()+wsdlLocation), serviceName);
						service = temp.getPort(NodeCommunicationWS.class);
						
						
						service.publish(poruka);

					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
		
		
	}
	
	@PostConstruct
	public void connect() {
		try {
			connection = (TopicConnection)connectionFactory.createConnection("guest", "guestguest");
			session = connection.createTopicSession(false,
			        Session.AUTO_ACKNOWLEDGE);
			connection.start();
			consumer = session.createConsumer(topic);
			consumer.setMessageListener(this);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@PreDestroy
	public void disconnect() {
		try {
			consumer.close();
			connection.stop();
			connection.close();
			session.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
